package com.java.comprehensive.arithmetic.leecode.easy;

public class LeetCode_541 {

    public static String reverseStr(String s, int k) {
        StringBuilder result = new StringBuilder(s);
        for (int i = 0; i < s.length(); i += 2 * k) {
            if (i + k <= s.length()) {
                StringBuilder reverse = new StringBuilder(s.substring(i, i + k)).reverse();
                result.replace(i, i + k, reverse.toString());
                continue;
            }
            StringBuilder reverse = new StringBuilder(s.substring(i, s.length())).reverse();
            result.replace(i, s.length(), reverse.toString());
        }
        return result.toString();
    }

    public static void main(String[] args) {
        String s = "abcdefg";
        int k = 8;

        System.out.println(reverseStr(s, k));
    }
}
